from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, DestroyModelMixin

from apps.models import Tag

from apps.serializers.Tag import TagSerializer
from apps.utils.common_mixin import APIListModelMixin, APIDeleteModelMixin, APICreateModelMixin
from apps.utils.common_response import APIResponse
from apps.views.main.user import make_login_log


class AdminTagView(GenericViewSet, APIListModelMixin, APICreateModelMixin):
    queryset = Tag.objects.all()
    serializer_class = TagSerializer

class AdminTagUpdateView(GenericViewSet):
    def create(self, request):
        try:
            id = request.GET.get('id')
            tag_data = Tag.objects.filter(id=id).first()
        except Tag.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        serializer = TagSerializer(tag_data, data=request.data)
        if serializer.is_valid():
            serializer.save()
        return APIResponse(code=0, msg='更新成功', data=serializer.data)

class AdminDeleteView(GenericViewSet):
    def create(self, request):
        try:
            ids = request.GET.get('ids')
            ids_arr = ids.split(',')
            # 批量删除
            Tag.objects.filter(id__in=ids_arr).delete()
        except Tag.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        return APIResponse(code=0, msg='删除成功')
